Creating a Filter, Edge Detection

Import resources and display image


In [1]:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

import cv2
import numpy as np

%matplotlib inline

# Read in the image
image = mpimg.imread(fname='images/curved_lane.jpg')

plt.imshow(X=image)


Out[1]:
<matplotlib.image.AxesImage at 0x200f1ad5e80>

Convert the image to grayscale


In [14]:
# Convert to grayscale for filtering
gray = cv2.cvtColor(src=image,
                    code=cv2.COLOR_RGB2GRAY)

plt.imshow(X=gray, cmap='gray')


Out[14]:
<matplotlib.image.AxesImage at 0x200f5d08fd0>

TODO: Create a custom kernel

Below, you've been given one common type of edge detection filter: a Sobel operator.

The Sobel filter is very commonly used in edge detection and in finding patterns in intensity in an image. Applying a Sobel filter to an image is a way of taking (an approximation) of the derivative of the image in the x or y direction, separately. The operators look as follows.

It's up to you to create a Sobel x operator and apply it to the given image.

For a challenge, see if you can put the image through a series of filters: first one that blurs the image (takes an average of pixels), and then one that detects the edges.


In [3]:
# Create a custom kernel

# 3x3 array for edge detection
sobel_y = np.array([[ -1, -2, -1],
                    [ 0, 0, 0], 
                    [ 1, 2, 1]])

## TODO: Create and apply a Sobel x operator
sobel_x = np.array([[ -1, 0, 1],
                    [ -2, 0, 2], 
                    [ -1, 0, 1]])

# Filter the image using filter2D, which has inputs: (grayscale image, bit-depth, kernel)  
filtered_image = cv2.filter2D(src=gray,
                              ddepth=-1,
                              kernel=sobel_y)
filtered_image2 = cv2.filter2D(src=gray,
                               ddepth=-1,
                               kernel=sobel_x)

plt.imshow(X=filtered_image,
           cmap='gray')


Out[3]:
<matplotlib.image.AxesImage at 0x200f41796a0>

In [4]:
plt.imshow(X=filtered_image2,
           cmap='gray')


Out[4]:
<matplotlib.image.AxesImage at 0x200f51a68d0>

Test out other filters!

You're encouraged to create other kinds of filters and apply them to see what happens! As an optional exercise, try the following:

  • Create a filter with decimal value weights.
  • Create a 5x5 filter
  • Apply your filters to the other images in the images directory.

In [5]:
image2 = mpimg.imread(fname='images/white_lines.jpg')
gray2 = cv2.cvtColor(src=image2,
                     code=cv2.COLOR_RGB2GRAY)

In [6]:
sample_filter = np.array([[ 0, 0, 2, 0, 0],
                          [ 0, 0, 1, 0, 0],
                          [ 2, -1, 1, -1, 2],
                          [ 0, 0, 1, 0, 0],
                          [ 0, 0, 2, 0, 0]])

In [7]:
filtered_image3 = cv2.filter2D(src=gray2,
                               ddepth=-1,
                               kernel=sample_filter)

In [8]:
plt.imshow(X=gray2,
           cmap='gray')


Out[8]:
<matplotlib.image.AxesImage at 0x200f58788d0>

In [11]:
plt.imshow(X=filtered_image3,
           cmap='gray')


Out[11]:
<matplotlib.image.AxesImage at 0x200f5cb8550>